<?php
	const PIC_PATH = './public/images/uface/';
	//用户管理者(控制器)
	function __construct(){
		require './model/user_model.php';//拿工具
		$GLOBALS['act']();               //去执行
	}
//------------------------------------------
    //显示登录页面
    function login(){
        require "./view/user/login.php";
    }

    //验证
    function dologin(){
		//先验证验证码是否匹配
		if(strtolower($_SESSION['code'])!=strtolower($_POST['code'])){
			echo '验证码不匹配!';
			echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=login' />";
            return;
		}
        //再验证用户名或密码是否匹配
		$condition = " WHERE uname='$_POST[uname]' AND upwd=md5('$_POST[upwd]')";
		if(select($condition)){
			$condition = " WHERE uname='$_POST[uname]' AND upwd=md5('$_POST[upwd]') AND auth=1";
			if(select($condition)){
				$_SESSION['flag'] = true;
				echo "<meta http-equiv=refresh content='0,./index.php?c=user&a=index' />";
				return;
			}else{
				echo '您没有权限访问!';
				echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=login' />";
				return;
			}     
		}
        
		echo '帐号或密码错误!';
        echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=login' />";
    }
	//退出登录
	function quit(){
		$_SESSION['flag'] = NULL;
		echo "<meta http-equiv=refresh content='0,./index.php' />";
	}
	//-------------------------------------------------------------
	function index(){
		//条件查找
		$condition = '';
		if(isset($_GET['auth']) && $_GET['auth']!='')  $while[] = " auth='$_GET[auth]' ";
		if(isset($_GET['uname'])&& $_GET['uname']!='' )$while[] = " uname LIKE '%$_GET[uname]%' ";
		if(isset($while)){
			$condition = ' WHERE '.implode(' AND ',$while);
		}
		//分页处理-------------------------------------------
		$setRows = 10;                     //每页条数
		$allRows = rowCount($condition);   //总条数
		$allPage = ceil($allRows/$setRows);//总页数
		
		$nowPage = empty($_GET['nowPage']) ? 1 : $_GET['nowPage'];//当前页
		
		$prevPage = $nowPage-1;
		$nextPage = $nowPage+1;
		
		if($prevPage < 1){
			$nowPage = 1;
			$prevPage = 1;
		}elseif($nextPage > $allPage){
			$nowPage = $allPage;
			$nextPage = $allPage;
		}

		$condition .=' LIMIT '.(($nowPage-1)*$setRows).','.$setRows;
		//-----------------------------------------------
		$ret = select($condition);       //取出数据
		require "./view/user/index.php"; //显示数据
	}
	
	function del(){//执行删除
		if($_GET['pic'] != 'normal.jpg'){
			@unlink(PIC_PATH.$_GET['pic']);
			@unlink(PIC_PATH.'sm_'.$_GET['pic']);
		}
		
		if(delete()){
			echo "<meta http-equiv='refresh' content=0;./index.php?c=user&a=index&nowPage=$_GET[nowPage]&auth=$_GET[auth]&uname=$_GET[uname]>";
		}else{
			echo '删除失败!';
			echo "<meta http-equiv='refresh' content=2;./index.php?c=user&a=index&nowPage=$_GET[nowPage]&auth=$_GET[auth]&uname=$_GET[uname]>";
		}
	}
	function auth(){
		$_POST['auth'] = $_GET['s'];
		if(update($_GET['id'])){
			echo "<meta http-equiv='refresh' content=0;./index.php?c=user&a=index&nowPage=$_GET[nowPage]&auth=$_GET[auth]&uname=$_GET[uname]>";
		}else{
			echo '删除失败!';
			echo "<meta http-equiv='refresh' content=2;./index.php?c=user&a=index&nowPage=$_GET[nowPage]&auth=$_GET[auth]&uname=$_GET[uname]>";
		}
	}
	
	function change(){
		$ret = select("WHERE $_GET[pk] = $_GET[id]")[0];//取出数据
		require './view/user/change.php';
	}
	function dochange(){
		
		require './public/tools/fileUpload.php';
		require './public/tools/picZoom.php';
		$res = fileUpload(PIC_PATH,$_FILES['uface'],50,50);
		if($res['flag']){
			$_POST['uface'] = $res['picname'];
		}
		
		if($_POST['upwd']!=$_POST['upwd2']){
			echo '密码不一致,请点击<b style="color:deeppink;text-decoration:underline" onclick="history.go(-1)">返回</b>重新修改!';
			return;
		}
		
		if(empty($_POST['upwd']) || empty($_POST['upwd2'])){
			unset($_POST['upwd']);
		}else{
			$_POST['upwd'] = md5($_POST['upwd']);
		}
		unset($_POST['upwd2']);
		
		if($_POST['birth']==''){
			unset($_POST['birth']);
		}else{
			$_POST['birth'] = strtotime($_POST['birth']);
		}
		
		if(update("WHERE $_GET[pk] = $_GET[id]")){
			echo "<meta http-equiv='refresh' content='0;./index.php?c=user&a=index&nowPage=$_GET[nowPage]&auth=$_GET[auth]&uname=$_GET[uname]'>";
		}else{
			echo '操作无效!您并没有改变该用户信息.';
			echo "<meta http-equiv='refresh' content='2;./index.php?c=user&a=index&nowPage=$_GET[nowPage]&auth=$_GET[auth]&uname=$_GET[uname]'>";
		}
	}
	
	function add(){
		require './view/user/add.php';
	}
	
	function doadd(){
		$setRows = 10;             //每页条数
		$allRows = rowCount()+1;   //总条数
		$allPage = ceil($allRows/$setRows);//总页数
		
		if($_POST['upwd']!=$_POST['upwd2']){
			echo '密码不一致!';
			echo "<meta http-equiv='refresh' content=2;./index.php?c=user&a=index&nowPage=$allPage>";
			return;
		}
		unset($_POST['upwd2']);
		$_POST['birth'] = strtotime($_POST['birth']);
		$_POST['upwd'] = md5($_POST['upwd']);
		
		require './public/tools/fileUpload.php';
		require './public/tools/picZoom.php';
		$res = fileUpload(PIC_PATH,$_FILES['uface'],50,50);
		if($res['flag']){
			$_POST['uface'] = $res['picname'];
		}
		
		if(insert()){
			echo "<meta http-equiv='refresh' content=0;./index.php?c=user&a=index&nowPage=$allPage>";
		}else{
			echo '增加失败!';
			echo "<meta http-equiv='refresh' content=2;./index.php?c=user&a=index&nowPage=$allPage>";
		}
	}